package com.simp.controller;

import com.simp.aop.SysLog;
import com.simp.dto.GroovyDto;
import com.simp.entity.SysScriptEntity;
import com.simp.model.SysScriptModel;
import com.simp.page.R;
import com.simp.page.SuperController;
import com.simp.service.GroovyService;
import com.simp.utils.BeanConvertUtils;
import com.simp.vo.GroovyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author pan.shun
 * @since 2022/3/22 15:52
 */
@Api(tags = "Groovy执行器")
@RestController
@RequestMapping("/groovy")
public class GroovyController extends SuperController {

    @Autowired
    private GroovyService groovyService;

    @ApiOperation(value = "脚本分页查询")
    @GetMapping("/list")
    public R getPageList(GroovyDto groovyDto) {
        return groovyService.getPageList(groovyDto);
    }

    @SysLog(value = "脚本", addOrUpdate = true)
    @ApiOperation("脚本新增与修改")
    @PostMapping("/addOrUpdateScript")
    public R addOrUpdateScript(@RequestBody SysScriptModel sysScriptModel, HttpServletRequest request) {
        SysScriptEntity convert = BeanConvertUtils.convert(sysScriptModel, SysScriptEntity.class);
        return groovyService.addOrUpdateScript(convert, request);
    }

    @SysLog(value = "脚本删除")
    @ApiOperation("通过ID删除脚本")
    @GetMapping("/removeScriptById/{id}")
    public R removeScriptById(@ApiParam(name = "脚本id", value = "脚本id", required = true) @PathVariable String id) {
        return groovyService.removeScriptById(id);
    }

    @SysLog(value = "运行文件参数脚本")
    @ApiOperation(value = "运行文件入参脚本")
    @PostMapping("/runFileScript")
    public R runFileScript(@ApiParam(name = "需要上传的文件", required = true) @RequestParam("file") MultipartFile file,
                           @ApiParam(name = "脚本ID", required = true) @RequestParam("id") String id) {
        return groovyService.runFileScript(file, id);
    }

    @SysLog(value = "运行普通脚本")
    @ApiOperation(value = "运行普通脚本")
    @PostMapping("/runScript")
    public R runScript(@RequestBody GroovyVo groovyVo) {
        return groovyService.runScript(groovyVo);
    }
}
